<?php
/**
 * 自定义辅助函数
 */

if(!function_exists('res'))
{
    function res($params = [], $type = 'json')
    {
        $responseData = [
            'status'    => isset($params['status']) ? $params['status'] : 200,
            'exception' => isset($params['exception']) ? $params['exception'] : '',
            'code'      => isset($params['code']) ? $params['code'] : 0,
            'message'   => isset($params['message']) ? $params['message'] : '',
            'data'      => !empty($params['data']) ? $params['data'] : (object)array(),
        ];

        //header('Content-type:application/json;charset=utf-8');
        //exit(json_encode($responseData));
        return $responseData;
    }
}

if(!function_exists('ok'))
{
    function ok($data = [], $msg = '')
    {
        if (is_array($data) && empty($data)) {
            $data = (object)array();
        }
        return res(['message' => $msg,'data' => $data]);
    }
}

if(!function_exists('err'))
{
    function err($code = 0, $status=200, $exception='', $msg='')
    {
        return res(['status'=>$status,'exception'=>$exception,'code'=>$code,'message'=>$msg]);
    }
}

if(!function_exists('sendPost'))
{
    function sendPost($url, $datas)
    {
        $temps = array();
        foreach ($datas as $key => $value) {
            $temps[] = sprintf('%s=%s', $key, $value);
        }
        $post_data = implode('&', $temps);
        $url_info = parse_url($url);
        if(empty($url_info['port']))
        {
            $url_info['port']=80;
        }
        $httpheader = "POST " . $url_info['path'] . " HTTP/1.0\r\n";
        $httpheader.= "Host:" . $url_info['host'] . "\r\n";
        $httpheader.= "Content-Type:application/x-www-form-urlencoded\r\n";
        $httpheader.= "Content-Length:" . strlen($post_data) . "\r\n";
        $httpheader.= "Connection:close\r\n\r\n";
        $httpheader.= $post_data;
        $fd = fsockopen($url_info['host'], $url_info['port']);
        fwrite($fd, $httpheader);
        $gets = "";
        $headerFlag = true;
        while (!feof($fd)) {
            if (($header = @fgets($fd)) && ($header == "\r\n" || $header == "\n")) {
                break;
            }
        }
        while (!feof($fd)) {
            $gets.= fread($fd, 128);
        }
        fclose($fd);

        return $gets;
    }
}

/**
 * 电商Sign签名生成
 * @param data 内容
 * @param appkey Appkey
 * @return DataSign签名
 */
function kdniao_encrypt($data, $appkey)
{
    return urlencode(base64_encode(md5($data.$appkey)));
}

/**
 * Json方式 查询订单物流轨迹
 */
function getOrderTracesByJson($express_code, $express_no, $order_code = ''){
    $requestData= "{'OrderCode':'{$order_code}','ShipperCode':'{$express_code}','LogisticCode':'{$express_no}'}";

    $datas = array(
        'EBusinessID' => '1363157',
        'RequestType' => '1002',
        'RequestData' => urlencode($requestData) ,
        'DataType' => '2',
    );
    $datas['DataSign'] = kdniao_encrypt($requestData, 'f426aae4-89fa-4a90-863c-08c96328b7f6');
    $result=sendPost('http://api.kdniao.com/Ebusiness/EbusinessOrderHandle.aspx', $datas);

    return $result;
}

//获取微信小程序物流状态
function getWechatExpressState($state)
{
    /**
     * 100001 	揽件阶段-揽件成功
     * 100002 	揽件阶段-揽件失败
     * 100003 	揽件阶段-分配业务员
     * 200001 	运输阶段-更新运输轨迹
     * 300002 	派送阶段-开始派送
     * 300003 	派送阶段-签收成功
     * 300004 	派送阶段-签收失败
     * 400001 	异常阶段-订单取消
     * 400002 	异常阶段-订单滞留
     */
    $name = '';
    switch ($state)
    {
        case 100001:
            $name = '揽件成功';
            break;
        case 100002:
            $name = '揽件失败';
            break;
        case 100003:
            $name = '分配业务员';
            break;
        case 200001:
            $name = '更新运输轨迹';
            break;
        case 300002:
            $name = '开始派送';
            break;
        case 300003:
            $name = '签收成功';
            break;
        case 300004:
            $name = '签收失败';
            break;
        case 400001:
            $name = '订单取消';
            break;
        case 400002:
            $name = '订单滞留';
            break;
    }
    return $name;
    return $name;
}


